#include <stdint.h>
#include <string.h>

#include "fx_power.h"
#include "fx_gpio.h"
#include "fx_spi.h"

#include "aoaoutil.h"
#include "rtthread.h"

#include "fx_zoom.h"

#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))

void fx_zoom_read_step_a(void)
{
    static uint32_t write_data[] = {0x00000CB2, 0x0B10803A, 0x00000000, 0x08008C84};
    static uint32_t read_data[ARRAY_LEN(write_data)] = {0};

    for (uint8_t i = 0; i < ARRAY_LEN(write_data); i++)
    {
        fx_spi_tr(write_data + i, read_data + i);
        if (i != (ARRAY_LEN(write_data) - 1))
        {
            rt_thread_mdelay(1);
        }
    }

    rt_kprintf("%s r2: 0x%08X\n", __FUNCTION__, read_data[2]);
}

void fx_zoom_read_step_b(void)
{
    static uint32_t write_data[] = {
        0x0A281510, 0x0C108002, 0x01201540, 0x0D009510, 0xFFAE1580,
        0x0E00955A, 0x00003FC6, 0x0F0095A2, 0x00000000, 0x0800BFD8,
    };
    static uint32_t read_data[ARRAY_LEN(write_data)] = {0};

    // write_data[4] = 0x04A91584; // min
    // write_data[4] = 0xFFAE1580; // max
    write_data[4] = 0x01571598;

    for (uint8_t i = 0; i < ARRAY_LEN(write_data); i++)
    {
        fx_spi_tr(write_data + i, read_data + i);
        if (i != (ARRAY_LEN(write_data) - 1))
        {
            rt_thread_mdelay(1);
        }
    }

    rt_kprintf("%s w4: 0x%08X, r6: 0x%08X\n", __FUNCTION__, write_data[4], read_data[6]);
}

void fx_zoom_read_step_c(void)
{
    static uint32_t write_data[] = {0x00000FC0, 0x08108022, 0x00000000, 0x08008FD2};
    static uint32_t read_data[ARRAY_LEN(write_data)] = {0};

    for (uint8_t i = 0; i < ARRAY_LEN(write_data); i++)
    {
        fx_spi_tr(write_data + i, read_data + i);
        if (i != (ARRAY_LEN(write_data) - 1))
        {
            rt_thread_mdelay(1);
        }
    }
}

void fx_zoom_read_step_d(void)
{
    static uint32_t write_data[] = {0x000009E8, 0x0910802A, 0x00000000, 0x080089FA};
    static uint32_t read_data[ARRAY_LEN(write_data)] = {0};

    for (uint8_t i = 0; i < ARRAY_LEN(write_data); i++)
    {
        fx_spi_tr(write_data + i, read_data + i);
        if (i != (ARRAY_LEN(write_data) - 1))
        {
            rt_thread_mdelay(1);
        }
    }
    rt_kprintf("%s r2: 0x%08X\n", __FUNCTION__, read_data[2]);
}

void fx_zoom_read_step_e(void)
{
    static uint32_t write_data[] = {0x00010842, 0x0A108032, 0x00000000, 0x08008874};
    static uint32_t read_data[ARRAY_LEN(write_data)] = {0};

    for (uint8_t i = 0; i < ARRAY_LEN(write_data); i++)
    {
        fx_spi_tr(write_data + i, read_data + i);
        if (i != (ARRAY_LEN(write_data) - 1))
        {
            rt_thread_mdelay(1);
        }
    }

    rt_kprintf("%s r2: 0x%08X\n", __FUNCTION__, read_data[2]);
}

void fx_zoom_read(void)
{
    fx_zoom_read_step_a();

    rt_thread_mdelay(2);

    fx_zoom_read_step_b();

    rt_thread_mdelay(1);

    fx_zoom_read_step_c();

    rt_thread_mdelay(1);

    fx_zoom_read_step_d();

    rt_thread_mdelay(1);

    fx_zoom_read_step_e();

    rt_thread_mdelay(25);
}
